package Avocado.form {

	import com.greensock.TweenMax;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;

	public class FormSelector extends MovieClip {
		
		private var _selected:Boolean;
		
		private var textLabel:String;
		private var spriteSelector:MovieClip;
		private var errorColor:uint;
		private var errorOpacity:Number;
		private var errorAmount:Number;
		private var selectorSpriteOpacity:Number;

		private var format:TextFormat;
		public var field:TextField;
		
		public function FormSelector(_spriteSelector:MovieClip, _format:TextFormat, _textLabel:String, _errorColor:uint=0xee0000, _errorOpacity:Number=1, _errorAmount:Number=1, _selectorSpriteOpacity:Number=1) {
			super();
			
			this.spriteSelector = _spriteSelector;
			this.format = _format;
			this.textLabel = _textLabel;
			this.errorColor = _errorColor;
			this.errorOpacity = _errorOpacity;
			this.errorAmount = _errorAmount;
			this.selectorSpriteOpacity = _selectorSpriteOpacity;
			
			this.addEventListener(Event.ADDED_TO_STAGE, initThis);
		}


		public function validate():Boolean{
			var valid:Boolean = new Boolean(false);
			if (this._selected == true) {
				TweenMax.to(spriteSelector, .33, {alpha: selectorSpriteOpacity, colorTransform:{tint:errorColor, tintAmount: 0}});
			} else {
				TweenMax.to(spriteSelector, .33, {alpha: errorOpacity, colorTransform:{tint:errorColor, tintAmount: errorAmount}});
			}
			return this._selected;
		}
		
		private function initThis(e:Event):void{
			this.removeEventListener(Event.ADDED_TO_STAGE, initThis);
			
			setupSelector();
			setupLabel();
			
			this.buttonMode = true;
			this.addEventListener(MouseEvent.MOUSE_DOWN, toggleThis);
		}
			public function set selected(_value:Boolean):void{
				this._selected = _value;
				updateVisual();
			}

			public function get selected():Boolean{
				return this._selected;
			}

			private function toggleThis(e:MouseEvent=null):void{
				if (this._selected == true){
					this._selected = false;
				} else {
					this._selected = true;
				}
				updateVisual();
			}
			
			private function updateVisual():void{
				TweenMax.to(spriteSelector, .33, {alpha: selectorSpriteOpacity, colorTransform:{tint:errorColor, tintAmount: 0}});
				if (this._selected == true){
					spriteSelector.gotoAndStop(2);
				} else {
					spriteSelector.gotoAndStop(1);
				}
			}

		private function setupSelector():void{
			spriteSelector.gotoAndStop(1);
			spriteSelector.alpha = this.selectorSpriteOpacity;
			addChild(spriteSelector);
		}
		

		private function setupLabel():void{
			field = new TextField;
			field.text = this.textLabel;
			field.autoSize = TextFieldAutoSize.LEFT;
			field.setTextFormat(format);
			field.embedFonts = true;
			field.selectable = false;
			field.x = 10;
			field.y = -10;
			addChild(field);
		}
		
	}
}